package com.wits.qso.web.controller;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.wits.comet.chat.service.CometContext;
import com.wits.comet.chat.service.CurrentLoginUser;
import com.wits.platform.common.utils.DateUtil;
import com.wits.platform.web.controller.BaseController;
import com.wits.qso.entity.Btable;
import com.wits.qso.entity.Student;
import com.wits.qso.service.BtableDaoService;
import com.wits.qso.service.StudentService;

@Controller
@RequestMapping(value="/qso/sign")
public class SignController extends BaseController{

	@Autowired
	private BtableDaoService btableDaoService;
	
	@Autowired
	private StudentService studentService;
	
	@RequestMapping(value="/toSign")
	public String toSign(Model model){
		String userId=this.getCurrentUserId();
		Btable btable=btableDaoService.findByTeaUserIdAndLessonTime(userId, DateUtil.getNow());
		String clsId=null;
		if(btable!=null){
			clsId=btable.getClaid().getClsId();
		}
		//从班级里选择学生
		List<Student> students= studentService.findByClsId(clsId);
		List<Student> offlineList=new ArrayList<Student>();
		if(students!=null && students.size()>0){
			CometContext cc=CometContext.getInstance();
			CurrentLoginUser clu=CurrentLoginUser.getInstance();
			Student stu=null;
			for(Iterator<Student> iter=students.iterator();iter.hasNext();){
				stu=iter.next();
				String memberId=stu.getStuUserId();
				if(cc.getMember(memberId)==null||clu.getMember(memberId)==null){
					offlineList.add(stu);
					iter.remove();
				}
			}
		}
		model.addAttribute("offlineList",offlineList);
		model.addAttribute("students",students);
		return "qso/sign/sign";
	}

}
